<template>
  <div 
    class="loading-spinner" 
    :class="[
      `loading-${type}`,
      `loading-${size}`,
      { 'loading-overlay': overlay }
    ]"
  >
    <div class="spinner-container">
      <!-- 默认旋转器 -->
      <div v-if="type === 'default'" class="spinner-default">
        <div class="spinner-circle"></div>
      </div>
      
      <!-- 点点旋转器 -->
      <div v-else-if="type === 'dots'" class="spinner-dots">
        <div class="dot"></div>
        <div class="dot"></div>
        <div class="dot"></div>
      </div>
      
      <!-- 脉冲旋转器 -->
      <div v-else-if="type === 'pulse'" class="spinner-pulse">
        <div class="pulse-circle"></div>
      </div>
      
      <!-- 波浪旋转器 -->
      <div v-else-if="type === 'wave'" class="spinner-wave">
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
        <div class="wave-bar"></div>
      </div>
      
      <!-- 环形旋转器 -->
      <div v-else-if="type === 'ring'" class="spinner-ring">
        <div class="ring"></div>
        <div class="ring"></div>
        <div class="ring"></div>
        <div class="ring"></div>
      </div>
      
      <!-- 方块旋转器 -->
      <div v-else-if="type === 'cube'" class="spinner-cube">
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
      </div>
      
      <!-- 文字加载 -->
      <div v-if="text" class="loading-text">{{ text }}</div>
    </div>
  </div>
</template>

<script setup>
// Props
defineProps({
  // 加载器类型
  type: {
    type: String,
    default: 'default',
    validator: (value) => ['default', 'dots', 'pulse', 'wave', 'ring', 'cube'].includes(value)
  },
  // 尺寸
  size: {
    type: String,
    default: 'medium',
    validator: (value) => ['small', 'medium', 'large'].includes(value)
  },
  // 加载文字
  text: {
    type: String,
    default: ''
  },
  // 是否显示遮罩
  overlay: {
    type: Boolean,
    default: false
  }
})
</script>

<style lang="scss" scoped>
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  
  &.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(2px);
    z-index: 2000;
  }
}

.spinner-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.loading-text {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  
  .loading-small & {
    font-size: 12px;
  }
  
  .loading-large & {
    font-size: 16px;
  }
}

// 默认旋转器
.spinner-default {
  .spinner-circle {
    width: 32px;
    height: 32px;
    border: 3px solid var(--el-border-color-lighter);
    border-top: 3px solid var(--el-color-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    
    .loading-small & {
      width: 24px;
      height: 24px;
      border-width: 2px;
    }
    
    .loading-large & {
      width: 48px;
      height: 48px;
      border-width: 4px;
    }
  }
}

// 点点旋转器
.spinner-dots {
  display: flex;
  gap: 4px;
  
  .dot {
    width: 8px;
    height: 8px;
    background: var(--el-color-primary);
    border-radius: 50%;
    animation: dots 1.4s ease-in-out infinite both;
    
    .loading-small & {
      width: 6px;
      height: 6px;
    }
    
    .loading-large & {
      width: 12px;
      height: 12px;
    }
    
    &:nth-child(1) { animation-delay: -0.32s; }
    &:nth-child(2) { animation-delay: -0.16s; }
    &:nth-child(3) { animation-delay: 0s; }
  }
}

// 脉冲旋转器
.spinner-pulse {
  .pulse-circle {
    width: 32px;
    height: 32px;
    background: var(--el-color-primary);
    border-radius: 50%;
    animation: pulse 1.5s ease-in-out infinite;
    
    .loading-small & {
      width: 24px;
      height: 24px;
    }
    
    .loading-large & {
      width: 48px;
      height: 48px;
    }
  }
}

// 波浪旋转器
.spinner-wave {
  display: flex;
  gap: 2px;
  
  .wave-bar {
    width: 4px;
    height: 24px;
    background: var(--el-color-primary);
    border-radius: 2px;
    animation: wave 1.2s ease-in-out infinite;
    
    .loading-small & {
      width: 3px;
      height: 18px;
    }
    
    .loading-large & {
      width: 6px;
      height: 36px;
    }
    
    &:nth-child(1) { animation-delay: -1.1s; }
    &:nth-child(2) { animation-delay: -1.0s; }
    &:nth-child(3) { animation-delay: -0.9s; }
    &:nth-child(4) { animation-delay: -0.8s; }
    &:nth-child(5) { animation-delay: -0.7s; }
  }
}

// 环形旋转器
.spinner-ring {
  position: relative;
  width: 32px;
  height: 32px;
  
  .loading-small & {
    width: 24px;
    height: 24px;
  }
  
  .loading-large & {
    width: 48px;
    height: 48px;
  }
  
  .ring {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 2px solid transparent;
    border-top: 2px solid var(--el-color-primary);
    border-radius: 50%;
    animation: ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
    
    &:nth-child(1) { animation-delay: -0.45s; }
    &:nth-child(2) { animation-delay: -0.3s; }
    &:nth-child(3) { animation-delay: -0.15s; }
    &:nth-child(4) { animation-delay: 0s; }
  }
}

// 方块旋转器
.spinner-cube {
  position: relative;
  width: 32px;
  height: 32px;
  
  .loading-small & {
    width: 24px;
    height: 24px;
  }
  
  .loading-large & {
    width: 48px;
    height: 48px;
  }
  
  .cube {
    position: absolute;
    width: 50%;
    height: 50%;
    background: var(--el-color-primary);
    animation: cube 2.4s ease-in-out infinite both;
    
    &:nth-child(1) {
      top: 0;
      left: 0;
      animation-delay: -1.1s;
    }
    
    &:nth-child(2) {
      top: 0;
      right: 0;
      animation-delay: -0.4s;
    }
    
    &:nth-child(3) {
      bottom: 0;
      left: 0;
      animation-delay: -0.7s;
    }
    
    &:nth-child(4) {
      bottom: 0;
      right: 0;
      animation-delay: 0s;
    }
  }
}

// 动画定义
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes dots {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(0);
    opacity: 1;
  }
  50% {
    transform: scale(1);
    opacity: 0.7;
  }
}

@keyframes wave {
  0%, 40%, 100% {
    transform: scaleY(0.4);
  }
  20% {
    transform: scaleY(1);
  }
}

@keyframes ring {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes cube {
  0%, 70%, 100% {
    transform: scale3D(1, 1, 1);
  }
  35% {
    transform: scale3D(0, 0, 1);
  }
}
</style>
